<template>
  <div v-if="isWeChatEnv" class="wechat" @click="getWechat">
    <div class="left-title">
      <i class="iconfont icon-wechat"></i>
      <div class="ET2">获取微信地址</div>
    </div>
    <i class="iconfont icon-zhixiang"></i>
  </div>
</template>

<script>
import { isWechat, isMP, isAliveIframe } from "../../utils/checkEnv";

export default {
  name: 'WechatAddress',
  props: {
    pageType: {
      type: String,
      default: "list"
    },
    orderdata: {
      type: String,
      default: ""
    },
    id: {
      type: [String, Number],
      default: ''
    },
    addressList: {
      type: Array,
      default: () => ([])
    }
  },
  computed: {
    isWeChatEnv() {
      // 微信h5或者视频号分销(鹅小店)小程序
      // 直播iframe不展示
      return !isAliveIframe && (isWechat || isMP);
    }
  },
  mounted() {

  },
  methods: {
    //微信初始化
    initWechatData() {
      this.$store.dispatch("initSdkInfo");
    },
    // 获取微信收货地址
    getWechat() {
      let that = this;
      window.wx.openAddress({
        success: function (res) {
          // 微信地址与地址列表匹配，不重复添加
          let compareRes = that.compareWechat(res);
          if (compareRes === "") {
            that.$emit("createAddress", res);
          } else {
            that.$emit("chooseAddress", compareRes);
          }
        },
        fail: function (err) {
          that.initWechatData();
        }
      });
    },
    // 微信地址与地址列表匹配，不重复添加
    compareWechat(wechatAddress) {
      let address = "";
      this.addressList.forEach(ele => {
        if (
          ele.receiver == wechatAddress.userName &&
          ele.phone == wechatAddress.telNumber &&
          ele.province == wechatAddress.provinceName &&
          ele.city == wechatAddress.cityName &&
          ele.county == wechatAddress.countryName &&
          ele.detail == wechatAddress.detailInfo
        ) {
          address = ele;
        }
      });
      return address;
    }
  },
};
</script>

<style lang="scss" scoped>
.wechat {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: white;
  padding: 0.32rem;
  margin-bottom: 0.16rem;
  box-sizing: border-box;

  .left-title {
    display: flex;
    align-items: center;

    .icon-wechat {
      color: #46bb36;
      font-size: 0.3rem;
      margin-right: 0.12rem;
    }
  }
}
</style>